package com.xbongbong.saas.enums.dictionary;

import com.xbongbong.i18n.util.I18nMessageUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 付款收款类型
 *
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/1/25 11:12
 * @since v1.0
 */
@Getter
public enum PayPlanStatusEnum {
    // 付款类型
    UN_RECEIVABLES("1", "", "enum.payPlanStatus.alias.un_receivables"),
    RECEIVABLES("2", "", "enum.payPlanStatus.alias.receivables"),
    PARTIAL_RECEIPTS("3", "", "enum.payPlanStatus.alias.partial_receipts"),
    BAD_DEBT("5", "isBad", "enum.payPlanStatus.alias.bad_debt"),
    PARTIAL_BAD_DEBT("6", "partialBad", "enum.payPlanStatus.alias.partial_bad_debt"),
    UNKNOWN("404", "", "enum.payPlanStatus.alias.unknown"),
    ;

    private String code;
    private String name;
    private String alias;

    PayPlanStatusEnum(String code, String name, String alias) {
        this.code = code;
        this.name = name;
        this.alias = alias;
    }

    /**
     * 获取未收款状态合集，此处包含：未收款、应收款、部分收款
     * @return java.util.List<java.lang.Integer> 未收款code集合
     * @author zcp
     * @date 2019/3/3 15:40
     * @since v1.0
     * @version v1.0
     */
    public static List<String> getUnStatusIn(){
        //未收款（应收款）、部分收款
        return Arrays.asList(UN_RECEIVABLES.code, PARTIAL_RECEIPTS.code);
    }

    /**
     * 通过枚举 code 获得枚举
     *
     * @param code 枚举Code
     *
     * @return PaymentStatusEnum
     */
    public static PayPlanStatusEnum getByCode(String code) {
        for (PayPlanStatusEnum cacheCode : values()) {
            if (code.equals(cacheCode.getCode())) {
                return cacheCode;
            }
        }
        return UNKNOWN;
    }

    public static boolean addPermission(String code) {
        return Objects.equals(code,UN_RECEIVABLES.code) || Objects.equals(code,PARTIAL_RECEIPTS.code);
    }

    public static PayPlanStatusEnum getByAlias(String alias) {
        for (PayPlanStatusEnum cacheCode : values()) {
            if (alias.equals(cacheCode.getAlias())) {
                return cacheCode;
            }
        }
        return UNKNOWN;
    }

    public static boolean invoicePermission(String code) {
        return Objects.equals(code,UN_RECEIVABLES.getCode()) || Objects.equals(code,PARTIAL_RECEIPTS.getCode()) || Objects.equals(code,RECEIVABLES.getCode());
    }

    /**
     * 获取坏账
     * @param status
     * @return
     */
    public static Integer getBad(String status){
        Integer isBad = 0;
        if ( Arrays.asList(BAD_DEBT.code, PARTIAL_BAD_DEBT.code).contains(status) ){
            isBad = 1;
        }
        return isBad;
    }

    public String getAlias() {
        return I18nMessageUtil.getMessage(alias);
    }
}
